import { ref, watch, reactive } from "vue"
import practiceRule from '../component/practiceRule.vue'
import customTypeName from '../component/customTypeName.vue'
import request from '@/utils/request'
import { Plus } from '@element-plus/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import $button from '@/button'
//题库类型列表
export function enableBankTypeListApi(dataRequest) {
    return request({
        url: "/course/bank/type/enable/list",
        method: "POST",
        data: dataRequest,
    });
}
// 规则分页
function rulePageApi(dataRequest) {
    return request({
        url: '/bank/rule/page',
        method: 'POST',
        data: dataRequest
    })
}
// 类目
export function configListApi(dataRequest) {
    return request({
        url: '/course/list',
        method: 'POST',
        data: dataRequest
    })
}
// 题库类型
export function configBankApi(dataRequest) {
    return request({
        url: '/course/bank/type/enable/list',
        method: 'POST',
        data: dataRequest
    })
}
// 新增 修改
export function ruleAddApi(dataRequest, meth) {
    return request({
        url: '/bank/rule',
        method: meth || 'POST',
        data: dataRequest
    })
}
// 删除
export function ruleDelApi(id) {
    return request({
        url: '/bank/rule/' + id,
        method: 'DELETE',
    })
}

// 详情
export function ruleDetailApi(id) {
    return request({
        url: '/bank/rule/' + id,
        method: 'GET',
    })
}

// 根据类别，题库类型，查询详细抽题规则
export function ruleByParentAndBankTypeApi(dataRequest) {
    return request({
        url: '/bank/rule/detail',
        method: 'POST',
        data: dataRequest
    })
}

// 上架预览
export function previewShowApi(dataRequest) {
    return request({
        url: '/bank/rule/all/course/bank/list',
        method: 'POST',
        data: dataRequest
    })
}


// 127.0.0.1:8080/bank/rule/30
export const tableItem = reactive({
    index: !true,//顺序
    tree: false,//树形
    loading: false,
    expandAll: true,//树形默认展开
    align: 'center',//对齐方式 全局
    column: [
        {
            label: '类别名称',
            prop: 'courseParentName',
            labelIcon: 'existUnConfigCourse',//打开标签显示
            newLine: true,
            component: customTypeName,
            labelIconStyle: {
                'color': '#f56c6c',
                'border-color': '#f56c6c',
            },
            // columnStyle: {
            //     'flex-direction': 'column',
            //     ' align-items': 'center',
            // }
        },
        {
            label: '题库类型',
            prop: 'courseBankTypeName',
        },
        {
            label: '练习题库规则',
            component: practiceRule,
            width: 420
        },
        {
            label: '测试题库规则',
            prop: 'testRuleText',
            clazz: 'margin-l-r-5px-warp',
            width: 420
        },
        {
            label: '规则创建日期',
            prop: 'createTime',
        },
        {
            label: '操作',
            prop: 'action',
        },
    ],
    pageInfo: {
        request: {
            "id": "",
            "name": "",
            "courseId": "",
            "courseBankTypeId": "",
            "pageDomain": {
                "pageNum": 1,//分页页面数 必须为数字
                "pageSize": 10,//分页页面数据大小  必须为数字
                "orderByColumn": "",//搜索参数
                "isAsc": "",//搜索参数
                "reasonable": ""//搜索参数
            }
        },
        url: '',//分页路由，不传的话需要传入自定义的分页函数paging 传入时定义的分页函数paging会失效
        method: 'POST',//分页请求方法，不传默认为POST
        // 分页函数paging
        paging: function () {
            tableItem.loading = true
            rulePageApi(tableItem.pageInfo.request).then(res => {
                tableItem.tableData.data = res.rows
                tableItem.tableData.total = res.total
                if (!tableItem.tableData.data || tableItem.tableData.data.length == 0) { return }
                tableItem.tableData.data.map((item) => {
                    //科目指定需要把子集全部展示出来
                    if (item.testConfModeTypeCode == "difference" && item.children) {
                        // console.log(item.children);
                        item.testRuleText = "科目指定\n"
                        for (let i = 0; i < item.children.length; i++) {
                            item.testRuleText += (`${item.children[i].courseName}：${item.children[i].testPracticePercent || 0}%练习题库+${item.children[i].testResiduePercent || 0}%全题库剩余\n`)
                        }
                    } else {
                        item.testRuleText = `统一指定：${item.testPracticePercent || 0}%练习题库+${item.testResiduePercent || 0}%全题库剩余`
                    }

                    if (item.practiceConfModeTypeCode == "difference" && item.children) {
                        // console.log(item.children);
                        item.practiceRuleText = "科目指定\n"
                        for (let i = 0; i < item.children.length; i++) {
                            item.children[i].practiceRuleText = (`${item.children[i].courseName}：全题库${item.children[i].practicePercent || 0}%试题\n`)
                        }
                    } else {
                        item.practiceRuleText = "统一指定：全题库" + item.practicePercent + "%试题"
                    }
                    // 全题库剩余；
                    // 共${item.testTotalAmount || 0}道题，总分${item.testTotalMark}
                    // item.existUnConfigCourse ? item.existUnConfigCourse = '有科目未配置' : ''
                })
                tableItem.loading = false
            }).catch(() => { tableItem.loading = false })
        }
    },
    // 分页数据 自定义分页函数paging时需要把值赋给tableData
    tableData: {
        total: '',//分页总计
        // 分页数据
        data: []
    },
    // 操作栏按钮
    btnArray: [
        {
            type: 'primary',//类别 摈弃了text
            label: '查看',
            plain: true,//镂空按钮  优先文字按钮
            text: true,//文字按钮
            hasPermi: [$button.trainbankRulequery || null],
            show: true,
            // 按钮方法
            handle: function (row) { }
        },
        {
            label: '修改',
            type: 'primary',
            hasPermi: [$button.trainbankRuleedit || null],
            show: true,
            text: true,
            handle: function (row) { }
        },
        {
            label: '预览',
            type: 'primary',
            // 查看的权限
            hasPermi: [$button.trainbankRulequery || null],
            show: true,
            text: true,
            handle: function (row) { }
        },
        {
            label: '删除',
            type: 'danger',
            // 查看的权限
            hasPermi: [$button.trainbankRuledel || null],
            show: true,
            text: true,
            handle: function (row) { }
        }
    ],
    moreBtnArrayText: "更多", //
    moreBtnArray: [],
    // 选择框触发
    // selectChange: function (row) {
    //     console.log(row)
    // },
}
)


